package com.wf.course.service.impl;
import java.io.IOException;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.utils.page.PageBean;
import com.wf.course.dao.IGoodsImageMapper;
import com.wf.course.dao.IGoodsInfoMapper;
import com.wf.course.dao.IGoodsParamMapper;
import com.wf.course.model.goods.GoodsImage;
import com.wf.course.model.goods.GoodsInfo;
import com.wf.course.model.goods.GoodsParam;
import com.wf.course.service.IGoodsInfoService;
import com.wf.course.web.Constants;
import com.wf.course.web.OSSUtil;

@Service
@Transactional(rollbackFor=Exception.class)
public class IGoodsInfoServiceImpl implements IGoodsInfoService{
	
	@Autowired
	private IGoodsInfoMapper mapper;
	@Autowired
	private IGoodsImageMapper imgMapper;
	@Autowired
	public IGoodsParamMapper paramMapper;

	@Override
	public void indexInfo(PageBean<GoodsInfo> pageBean) {
		// TODO Auto-generated method stub
		pageBean.setResults(this.mapper.queryList(pageBean));
		pageBean.setTotalCount(this.mapper.getTotalCount(pageBean).intValue());
	}

	@Override
	public void saveOrUpdateGoodsInfo(GoodsInfo goodsInfo,MultipartFile imgFile,
			GoodsImage goodsImage, MultipartFile[] goodsImgFiles,String detail,String[] paramValues) {
		// TODO Auto-generated method stub
		if(goodsInfo.getId()==null){
			
			//商品主图
			if (!imgFile.isEmpty()) {
				String goodsImg = "";
				try {
					goodsImg = OSSUtil.addFile(imgFile, "wf_goods/goodsImg", Constants.IGM_BUCKET);
				} catch (IOException e) {
					e.printStackTrace();
				}
				goodsInfo.setGoodsImg(goodsImg);
				goodsInfo.setGoodsSaleNumber(0);//商品销量默认为0
			}
			this.mapper.saveGoodsInfo(goodsInfo);
			
			//保存轮播图片
			 if(goodsImgFiles!=null&&goodsImgFiles.length>0){ 
				
				  String lunboImg = "";
				  for(int i = 0;i<goodsImgFiles.length;i++){  
		                MultipartFile file = goodsImgFiles[i]; 
		                try {
		                	lunboImg = OSSUtil.addFile(file, "wf_goods/goodsImg", Constants.IGM_BUCKET);
						} catch (IOException e) {
							e.printStackTrace();
						}
		             GoodsImage   goodsImage1 = new GoodsImage();
		             goodsImage1.setGoodsImg(lunboImg);
		             goodsImage1.setGoodsId(goodsInfo.getId());
		             goodsImage1.setOrderNum(i);
		                
		                imgMapper.saveGoodsImage(goodsImage1);
		            } 
			 }
			 
			 //保存商品参数：详情
			 GoodsParam goodsParam = new GoodsParam();
			 goodsParam.setGoodsId(goodsInfo.getId());
			 goodsParam.setShowFlag(0);
			 goodsParam.setParamName("内容简介");
			 goodsParam.setParamValue(detail);
			 goodsParam.setOrderNum(1);
			 paramMapper.saveGoodsParam(goodsParam);
			 
			 //保存商品规格
			 if(paramValues!=null && paramValues.length>0){
				 for(int i=0;i<paramValues.length;i++){
					 GoodsParam gm = new GoodsParam();
					 gm.setGoodsId(goodsInfo.getId());
					 gm.setShowFlag(1);
					 gm.setParamName("规格");
					 gm.setParamValue(paramValues[i]);
					 gm.setOrderNum(i);
					 paramMapper.saveGoodsParam(gm);
				 }
			 }
			 
			 
			
		}else{//修改
			
			//修改商品主图
			if (imgFile!=null && imgFile.getSize() != 0) {
				GoodsInfo inf = mapper.queryGoodsInfoById(goodsInfo.getId());
				try{
					if(StringUtils.isNotBlank(inf.getGoodsImg())){
						// 删除原来图片
						OSSUtil.delete(inf.getGoodsImg(), Constants.IGM_BUCKET);
					}
				}catch(Exception e){
					System.out.println(e.getMessage());
				}
				// 重新上传
				String goodsImg = "";
				try {
					goodsImg = OSSUtil.addFile(imgFile, "wf_goods/goodsImg", Constants.IGM_BUCKET);
				} catch (IOException e) {
					e.printStackTrace();
				}
				goodsInfo.setGoodsImg(goodsImg);
			}
			this.mapper.updateGoodsInfo(goodsInfo);
			
			// 修改商品轮播图
			 
			if(goodsImgFiles!=null&&goodsImgFiles.length>0 &&goodsImgFiles[0].getSize()>0){ 
				
				List<GoodsImage> lunboImages = imgMapper.queryGoodsImageByGoodsId(goodsInfo.getId());
				//如果轮播图存在，则删除  
				if(lunboImages!=null && lunboImages.size()>0){
					for(int i=0;i<lunboImages.size();i++){
						if(StringUtils.isNotBlank(lunboImages.get(i).getGoodsImg())){
							// 删除原来图片
							OSSUtil.delete(lunboImages.get(i).getGoodsImg(), Constants.IGM_BUCKET);
						}
					}
					imgMapper.deleteByGoodsId(goodsInfo.getId());
				  }
				
				//重新上传
				 String lunboImg = "";
				  for(int i = 0;i<goodsImgFiles.length;i++){  
		                MultipartFile file = goodsImgFiles[i]; 
		                try {
		                	lunboImg = OSSUtil.addFile(file, "wf_goods/goodsImg", Constants.IGM_BUCKET);
						} catch (IOException e) {
							e.printStackTrace();
						}
		                GoodsImage   goodsImage1 = new GoodsImage();
			             goodsImage1.setGoodsImg(lunboImg);
			             goodsImage1.setGoodsId(goodsInfo.getId());
			             goodsImage1.setOrderNum(i);
			                
			             imgMapper.saveGoodsImage(goodsImage1);
		            } 
				
			}
			
			// 更新商品参数：详情
			 List<GoodsParam> goodsParams = paramMapper.queryGoodsParamByCondition(goodsInfo.getId(),0);
			 if(goodsParams!=null && goodsParams.size()>0){
				 
				 GoodsParam goodsParam = new GoodsParam();
				 goodsParam.setId(goodsParams.get(0).getId());
				
				 goodsParam.setParamValue(detail);
				 paramMapper.updateGoodsParam(goodsParam);
			 }
			 
			 
			 //更新商品规格，先删除，再添加
			 
			 if(paramValues!=null && paramValues.length>0){
				 
				 List<GoodsParam> goodsParamsGuige = paramMapper.queryGoodsParamByCondition(goodsInfo.getId(),1);
				 if(goodsParamsGuige!=null && goodsParamsGuige.size()>0){
					 for(int i=0;i<goodsParamsGuige.size();i++){
						 paramMapper.deleteById(goodsParamsGuige.get(i).getId());
					 }
				 }
				 
				 for(int i=0;i<paramValues.length;i++){
					 GoodsParam gm = new GoodsParam();
					 gm.setGoodsId(goodsInfo.getId());
					 gm.setShowFlag(1);
					 gm.setParamName("规格");
					 gm.setParamValue(paramValues[i]);
					 gm.setOrderNum(i);
					 paramMapper.saveGoodsParam(gm);
				 }
			 }
			
		}
		
	}

	@Override
	public GoodsInfo queryGoodsInfoById(Integer id) {
		// TODO Auto-generated method stub
		return this.mapper.queryGoodsInfoById(id);
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		this.mapper.deleteById(id);
	}

	@Override
	public List<GoodsInfo> queryGoods(){
		return this.mapper.queryGoods();
	}
	
}
